package il.ac.braude.sona.control;

import il.ac.braude.sona.constants.Mssg;
import il.ac.braude.sona.constants.P;
import il.ac.braude.sona.constants.Resources;
import il.ac.braude.sona.constants.TestingParamKit;
import il.ac.braude.sona.entities.ChangeRequest;
import il.ac.braude.sona.gui.forms.FormFrame;
import il.ac.braude.sona.gui.forms.FormListener;
import il.ac.braude.sona.gui.forms.myforms.EnterReqIdForm;
import il.ac.braude.sona.gui.infogui.RequsetInfoFrame;
import il.ac.braude.sona.gui.mytables.ChangeRequestTableFrame;
import il.ac.braude.sona.gui.mytables.ChangeRequestTableModel;
import il.ac.braude.sona.ocsf.client.Client;
import il.ac.braude.sona.ocsf.client.ServerResponseListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
/**
 * The ViewRequestController contains all methods to initiate the requests view frames
 *  
 * @author Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rosenthal
 */
public class ViewRequestController implements FormListener {
	/**
	 * The fram for entering request ID
	 */
	public EnterReqIdForm viewReqForm;
	/**
	 * Params for fitTesting
	 */
	private RequsetInfoFrame testedReqFrame=null;
	private HashMap<String,String> req;
	private ChangeRequest testedChangeReq;
	private TestingParamKit myKit=TestingParamKit.getTestingKit();


	
	/**
	 * initiate new fram for entering request number
	 */
	public void execViewChangeRequest() {
		if (viewReqForm == null) {
			viewReqForm = new EnterReqIdForm();
			viewReqForm.addFormListener(ViewRequestController.this);
			if(myKit.TestMode)
				myKit.notifyTester();
			else
			viewReqForm.showFrame();
		} else
			if(myKit.TestMode)
				myKit.notifyTester();
			else
			viewReqForm.toFront();
	}

	/**
	 * Asking server for request data information and 
	 * initiate new fram that shows request monitoring details 
	 * 
	 * @param id - The request id to view
	 */
	public void viewReqById(String id) {
		try {
			Mssg mssg = new P.ViewMssg(P.ViewMssg.view_change_request_by_id, id);
			viewReqForm.showMessege(Resources.Strings.Connecting);
			Client.getInstance().sendToServer(new ServerResponseListener() {

				@SuppressWarnings("unchecked")
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						viewReqForm.showMessege((String) mssg.getMessege());
						return;
					}
					if(myKit.TestMode) {
					req = (HashMap<String,String>) mssg.getMessege();
						myKit.notifyTester();
				}
					else
					if ((req = (HashMap<String,String>) mssg.getMessege()) != null) {
						new RequsetInfoFrame(req);
					} else
						JOptionPane.showMessageDialog(viewReqForm,
								String.format(Resources.Strings.NoSuchRequest));
					
					viewReqForm.dispose();
					viewReqForm = null;
				}
			}, mssg);

		} catch (Exception e) {
			e.printStackTrace();
			viewReqForm.dispose();
			viewReqForm = null;
		}
	}
	/**
	 * When user asks for viewing request from messages inbox:
	 * Asking server for request data information and 
	 * initiate new fram that shows request full details 
	 * 
	 * @param id - The request id to view
	 */
	public void viewReqFromMssg(String id) {
		try {
			Mssg mssg = new P.ViewMssg(P.ViewMssg.viewChangeRequestForMsg, id);
			Client.getInstance().sendToServer(new ServerResponseListener() {

				@Override
				public void onServerResponse(Mssg mssg) {

					if (mssg.is(P.error)) {
						JOptionPane.showMessageDialog(null,mssg.getMessege());
						return;
					}
					if ((testedChangeReq = (ChangeRequest) mssg.getMessege()) != null) {
						setTestReqFrame(new RequsetInfoFrame(testedChangeReq));
					} else
						JOptionPane.showMessageDialog(null,"Error From SQL");
					if (myKit.TestMode) 
						myKit.notifyTester();
				}
			}, mssg);

		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, Resources.Strings.ConnectFailed);
		}
	}


	/**
	 * Asking server for all request data information and 
	 * initiate table view that shows all request
	 */
	public void execViewAllChangeRequests() {
		try {
			HashMap<String, String> hash = new HashMap<String, String>();
			hash.put("key", "view all requests");
			Mssg msg = new P.ViewMssg(P.ViewMssg.view_all_requests, null);
			final ChangeRequestTableFrame tableFrame = new ChangeRequestTableFrame();
			tableFrame.add(new JLabel(Resources.Strings.Connecting,SwingConstants.CENTER));
			Client.getInstance().sendToServer(new ServerResponseListener() {
				@SuppressWarnings("unchecked")
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						JOptionPane.showMessageDialog(null,
								(mssg.getMessege()));
						return;
					}
					tableFrame.setTable(new ChangeRequestTableModel((ArrayList<ChangeRequest>) mssg.getMessege()));
					tableFrame.showFrame();
				}
			}, msg);

		} catch (IOException e) {
			JOptionPane
					.showMessageDialog(null, Resources.Strings.ConnectFailed);
		}
	}

	@Override
	public void onFormCanceled(FormFrame formFrame) {
		if (viewReqForm!=null) viewReqForm.dispose();
		viewReqForm = null;
	}
	
	@Override
	public void onFormSubmited(FormFrame formFrame, ArrayList<Object> values) {
		if (formFrame == viewReqForm)
			viewReqById(values.get(0).toString());
	
	}

	public RequsetInfoFrame getTestReqFrame() {
		return testedReqFrame;
	}

	public void setTestReqFrame(RequsetInfoFrame testReq) {
		this.testedReqFrame = testReq;
	}
	public ChangeRequest getTestingChangeReq() {
		return testedChangeReq;
	}

	public void setTestingChangeReq(ChangeRequest testReq) {
		this.testedChangeReq = testReq;
	}
	public HashMap<String,String> getFields() {
		return this.req;
	}



}
